using System.Collections.Generic;
using UnityEngine;

namespace Gj_sparrow.Framework.Scripts.MapData
{
    public class SizeGrid
    {
	    public Vector2Int center;
	    public Vector2Int size;
	    public Vector2Int start;
	    public short BoundsMaxX;
	    public short BoundsMinX;
	    public short BoundsMaxY;
	    public short BoundsMinY;

	    public SizeGrid(Vector2Int center, Vector2Int size)
	    {
		    start = new Vector2Int();
		    if (center.x != 0)
		    {
			    start.x = center.x;
			    start.y = center.y;
		    }
		    else
		    {
			    start.x = center.x - size.x / 2;
			    start.y = center.y - size.y / 2;
		    }

		    start = cent(ref size);
		    BoundsMaxX = (short)(size.x - 1);
		    BoundsMinX = 0;
		    BoundsMaxY = (short)(size.y - 1);
		    BoundsMinY = 0;
		    // Debug.LogError(start+":"+BoundsMaxX+":"+BoundsMaxY);
	    }

	    private Vector2Int cent(ref Vector2Int p)
	    {
		    var _px = p.x / 2 * -1;
		    var _py = p.y / 2 * -1;
		    var o = new Vector2Int { };
		    if (_px%2 != 0)
		    {
			    o.x = (_px - 1);
		    } else
		    {
			    o.x = _px;
		    }
		    if (_py%2 > 15)
		    {
			    o.y = (_py - 1);
		    } else
		    {
			    o.y = _py;
		    }
		    return o;
	    }

	    public void Tailor(ref Vector2Int point)
	    {
		    point.x -= start.x;
		    point.y -= start.y;
	    }

	    public void Tailor(List<Vector2Int> nodes)
	    {
		    for (var i = 0; i < nodes.Count; i++)
		    {
			    var node = nodes[i];
			    node.x -= start.x;
			    node.y -= start.y;
			    nodes[i] = node;
		    }
	    }
	    public void Tailor(Vector2Int[] nodes)
	    {
		    for (var i = 0; i < nodes.Length; i++)
		    {
			    var node = nodes[i];
			    node.x -= start.x;
			    node.y -= start.y;
			    nodes[i] = node;
		    }
	    }
	    public void Restore(ref Vector2Int point)
	    {
		    point.x += start.x;
		    point.y += start.y;
	    }
	    
	    public void Restore3(ref Vector3Int point)
	    {
		    point.x += start.x;
		    point.y += start.y;
	    }

	    public void Restore(List<Vector2Int> nodes)
	    {
		    for (var i = 0; i < nodes.Count; i++)
		    {
			    var node = nodes[i];
			    node.x += start.x;
			    node.y += start.y;
			    nodes[i] = node;
		    }
	    }
	    
	    public void Restore(Vector2Int[] nodes)
	    {
		    for (var i = 0; i < nodes.Length; i++)
		    {
			    nodes[i].x = nodes[i].x + start.x;
			    nodes[i].y = nodes[i].y + start.y;
		    }
	    }

	    public bool InBounds(int x, int y)
	    {
		    return x > BoundsMinX && x <= BoundsMaxX && y > BoundsMinY && y <= BoundsMaxY;
	    }

	    public uint ToIndex(ref Vector2Int end)
	    {
		    var x = (uint)(end.x - start.x);
		    var y = (uint)(end.y - start.y);
		    return (x * (uint)(size.x) * 2 + y);
	    }

	    public uint[] ToIndex(Vector2Int[] slice)
	    {
		    var result = new uint[slice.Length];
		    for (var i = 0; i < slice.Length; i++)
		    {
			    result[i] = ToIndex(ref slice[i]);
		    }
		    return result;
	    }
    }
}